//初始化
$(document).ready(function() {
	setDefActor();
	// init();
});

/**
 * id查询
 * @param s
 * @returns
 */
function getById(s) {
	return document.getElementById(s);
}

/**
 * name查询
 * @param s
 * @returns
 */
function getByName(s) {
	return document.getElementsByName(s);
}

/**
 * 定义$each
 */
Array.prototype.$each = function(fun) {
	for (var _i = 0; _i < this.length; _i++) {
		fun(this[_i]);
	}
}

/**
 * 嵌套循环lista和listb,当funa返回true时 执行funb
 * @param lista
 * @param listb
 * @param funa
 * @param funb
 */
function btfor(lista, listb, funa, funb) {
	for (var _i = 0; _i < lista.length; _i++) {
		for (var _j = 0; _j < listb.length; _j++) {
			if (funa(lista[_i], listb[_j])) {
				funb(lista[_i], listb[_j]);
			}
		}
	}
}

/**
 * 根据角色展示默认的功能模块权限
 */
function setDefActor() {
	//默认角色不能删除
	/*btfor(userActors, getByName("actorsArr"), function(a, b) {
		return a.defaultFlag == "0" && a.id == b.value
	}, function(a, b) {
		b.disabled = true;
	});*/
	
	//cover('t_funs');
	setFuns();
}

/**
 * 设置功能模块
 */
function setFuns() {
	userActors.$each(function(act) {
		if (act.flag == "1") {
			btfor(act.funs.split(","), getByName("funCodesArr"),
					function(a, b) {
						return a == b.value
					}, function(a, b) {
						b.checked = true;
						b.disabled = true;
					});
		}
	})
}

/**
 * 取消功能模块赋权
 */
function removeFuns() {
	var funs = getByName("funCodesArr");
	for (var i = 0; i < funs.length; i++) {
		if (funs[i].disabled) {
			funs[i].checked = false;
			funs[i].disabled = false;
		}
	}
}

/**
 * 选取角色市触发事件
 * @param actid
 * @param sta
 */
function changeActor(actid, sta) {
	var ifFound = false;
	var newActor;
	if (userActors.length == 0) {
		actors.$each(function(act) {
			if (act.id == actid)
				newActor = act;
		});
	}
	btfor(actors, userActors, function(a, b) {
		return a.id == actid
	}, function(a, b) {
		if (a.name == b.name && a.id != b.id) {
			b.flag = sta ? "0" : "1";
		} else if (a.name == b.name && a.id == b.id) {
			b.flag = sta ? "1" : "0";
			ifFound = true;
		}
		newActor = a;
	});
	if (!ifFound) {
		userActors.push(newActor);
	}
	removeFuns();
	setFuns();
}

/**
 * 保存
 */
function doSubmit() {
	/*userActors.$each(function(act) {
		if (act.flag == "1") {
			var hidAct = document.createElement("input");
			hidAct.type = "hidden";
			hidAct.name = "actorCodesStr";
			hidAct.value = act.id;
			document.getElementById("actor-user-form").appendChild(hidAct);
		}
	});*/
	var actorCodesStr = "";
	$('input[name="actorsArr"]').each(function(){
		if($(this).is(":checked")){
			if(actorCodesStr==""){
				actorCodesStr = $(this).val();
			} else {
				actorCodesStr += ',' + $(this).val();
			}
		}
	});
	var hidAct = document.createElement("input");
	hidAct.type = "hidden";
	hidAct.name = "actorCodesStr";
	hidAct.value = actorCodesStr;
	document.getElementById("actor-user-form").appendChild(hidAct);

	var dataJson = JSON.stringify(getFormJson("actor-user-form"));
	$.ajax({
		data : dataJson,
		url : $("#actor-user-form").attr('action'),
		type : "POST",
		dataType : "json",
		contentType : 'application/json; charset=UTF-8',
		async : true,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			printMsg(errorThrown, textStatus);
		},
		success : function(data) {
			var result = eval(data);
			if (result.resultCode == '1') {
				layer.alert('保存成功',function(index){
        			parent.$("#searchForm").submit();
        			closeWin();
        		});
			} else {
				var message = result.message;
				if (message != "") {
					layer.msg(message, {
						icon : 2,
						time : 1000
					});
				} else {
					layer.alert('保存失败');
				}
			}
		}
	})

}

/**
 * 设置功能权限的样式
 * @param tag  功能菜单li标签id
 */
function cover(tag) {
	var coverObj;
	if (getById("coverDiv")) {
		coverObj = getById("coverDiv");
	} else {
		coverObj = document.createElement("div");
		document.body.appendChild(coverObj);
		coverObj.setAttribute('id', 'coverDiv');
		coverObj.style.position = "absolute";
		coverObj.style.background = "#777";
		coverObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=40,finishOpacity=40)";
	}
	var e = getById(tag);
	var x = e.offsetLeft, y = e.offsetTop, w = e.offsetWidth, h = e.offsetHeight;
	while (e = e.offsetParent) {
		x += e.offsetLeft;
		y += e.offsetTop;
	}
	coverObj.style.top = y;
	coverObj.style.left = x;
	coverObj.style.width = w;
	coverObj.style.height = h;
}

// 选择部分函数
function deployBuilding(divCode) {
	if (!divCode)
		divCode = window.event.srcElement.code;
	getById("img_" + divCode).src = "../images/tree_minus.gif";
	getById("bui_" + divCode).style.display = "";
	getById("img_" + divCode).onclick = undeployBuilding;
}

function undeployBuilding() {
	divCode = window.event.srcElement.code;
	getById("img_" + divCode).src = "../images/tree_plus.gif";
	getById("bui_" + divCode).style.display = "none";
	getById("img_" + divCode).onclick = deployBuilding;
}

function checkDiv(code, ifcheck) {
	var l_buildings = getById("bui_" + code).firstChild.children;
	for (var i = 0; i < l_buildings.length; i++) {
		if (l_buildings[i].type == "checkbox")
			l_buildings[i].checked = ifcheck;
	}
}

function coverBuilding() {
	var acts = getByName("funCodesArr");
	var flag = false;
	for (var i = acts.length - 1; i >= 0; i--) {
		if (acts[i].value == "9901") {
			if (acts[i].checked) {
				flag = true;
			}
			break;
		}
	}
	if (flag) {
		var buildingCoverObj;
		if (getById("buildingCoverDiv")) {
			getById("buildingCoverDiv").style.display = "";
			buildingCoverObj = getById("buildingCoverDiv");
		} else {
			buildingCoverObj = document.createElement("div");
			document.body.appendChild(buildingCoverObj);
			buildingCoverObj.setAttribute('id', 'buildingCoverDiv');
			buildingCoverObj.style.position = "absolute";
			buildingCoverObj.style.background = "#777";
			buildingCoverObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=40,finishOpacity=40)";
		}
		var e = getById("t_buildings");
		var x = e.offsetLeft, y = e.offsetTop, w = e.offsetWidth, h = e.offsetHeight;
		while (e = e.offsetParent) {
			x += e.offsetLeft;
			y += e.offsetTop;
		}

		buildingCoverObj.style.top = y;
		buildingCoverObj.style.left = x;
		buildingCoverObj.style.width = w;
		buildingCoverObj.style.height = h;
	} else {
		if (getById("buildingCoverDiv"))
			getById("buildingCoverDiv").style.display = "none";
	}
}
